﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SharedLib.WinFormMVCFramework
{
    public static class ControlExtensions
    {
        /// <summary>
        /// Invoke method on UI thread
        /// </summary>
        /// <param name="method"></param>
        public static void InvokeOnUIThread(this Control control, Action method)
        {
            if (control.IsDisposed) return;
            if (control.InvokeRequired)
            {
                try
                {
                    control.Invoke(method);
                }
                catch (ObjectDisposedException ex)
                {
                    Console.WriteLine("Warning: Attempt to access disposed control detected: " + ex.Message);
                }
            }
            else
            {
                method();
            }
        }
    }
}
